﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.PrizeCommand;
	import com.wandian.controller.SocketCommand;
	import com.wandian.data.GameData;
	import com.wandian.model.UserInfoProxy;
	import com.wandian.model.vo.PropVo;
	import com.wandian.model.vo.VIPVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.util.StopWatch;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.FacePanel;
	import com.wandian.view.components.ui.FriendPanel;
	import com.wandian.view.components.ui.HornPanel;
	import com.wandian.view.components.ui.NationalDayPanel;
	import com.wandian.view.components.ui.PayBagPanel;
	import com.wandian.view.components.ui.PersonInfoMc;
	import com.wandian.view.components.ui.RedBagPanel;
	import com.wandian.view.components.ui.ScrollBarTxt;
	import com.wandian.view.components.ui.mingame.sgj.MaxWinPanel;
	import com.wandian.view.components.ui.mingame.zp.ZPPanel;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class SelectPersonMediator extends Mediator
	{
		private var _tabBar:CLZTabBar;
		private var _contenMc:Sprite;
		private var _global:GameData;
		private var _arrPerson:Array;
		public static const NAME:String="SelectPersonMediator";

		public function SelectPersonMediator(param1:Object=null)
		{
			this._contenMc=new Sprite();
			this._global=GameData.getInstance();
			this._arrPerson=[[], [], []];
			super(NAME, param1);
			return;
		}

		override public function onRegister():void
		{
			var prop:PropVo=null;
			facade.registerCommand(CLZNoticeName.UPDATE_PERSON_ID_REQ, SocketCommand);
			this.mcPanel.title_txt.text=GameData.tip["UPDATE_PERSON_TITLE"];
			AddBtn.initBtn(this.mcPanel, CLZNoticeName.HIDE_SELECTED_PERSON, AddBtn.CLOSE_TWO, "", 4, 1);
			this.mcPanel.addChild(this._contenMc);
			this._contenMc.x=31;
			this._contenMc.y=91;
			var tabs:Array=[[this.mcPanel.grilBtn, GameData.tip["GRIL"]], [this.mcPanel.boyBtn, GameData.tip["BOY"]], [this.mcPanel.otherBtn, GameData.tip["CARTOON"]]];
			this._tabBar=new CLZTabBar(tabs);
			var index:int=0;
			while (index < tabs.length)
			{

				tabs[index][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
				index=index + 1;
			}
			var arrProp:Array=GameData.getPcateProp(GameData.PROP_PCATE_ID_PERSON);
			index=0;
			while (index < arrProp.length)
			{

				prop=arrProp[index] as PropVo;
				this._arrPerson[prop.type].push(prop);
				index=index + 1;
			}
			if (this._global.userInfo.userVo.sex == 1)
			{
				this._tabBar.setClickIndex(this.mcPanel.grilBtn);
			}
			else
			{
				this._tabBar.setClickIndex(this.mcPanel.boyBtn);
			}
			return;
		}

		private function changeTabBarHandler(event:Event):void
		{
			switch (event.target.name)
			{
				case "grilBtn":
				{
					this.showPicList(this._arrPerson[1]);
					break;
				}
				case "boyBtn":
				{
					this.showPicList(this._arrPerson[2]);
					break;
				}
				case "otherBtn":
				{
					this.showPicList(this._arrPerson[0]);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function showPicList(arrPerson:Array):void
		{
			var mcPic:MovieClip=null;
			var mcPerson:PersonInfoMc=null;
			GameData.removeNumChildre(this._contenMc);
			var listScroll:CLZLlistScrollPanel=new CLZLlistScrollPanel(4, 163, 214, 20, 1, 430, 722);
			this._contenMc.addChild(listScroll);
			var total:int=arrPerson.length;
			var index:int=0;
			while (index < total)
			{

				mcPic=GameData.getMovieClip("PicMc");
				listScroll.setMc(mcPic, index, total);
				mcPerson=new PersonInfoMc(mcPic, arrPerson[index]);
				index=index + 1;
			}
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.HIDE_SELECTED_PERSON, CLZNoticeName.UPDATE_PERSON_ID_TIP, CLZNoticeName.UPDATE_PERSON_ID_REQ, CLZNoticeName.UPDATE_PERSON_ID_RES];
		}

		override public function handleNotification(notification:INotification):void
		{
			var msg:String=null;
			var personProp:PropVo=null;
			var tipTitle:String=null;
			var tipData:Object=null;
			switch (notification.getName())
			{
				case CLZNoticeName.HIDE_SELECTED_PERSON:
				{
					if (Texas.I._mcWindosView.contains(this.mcPanel))
					{
						Texas.I._mcWindosView.removeChildTo(this.mcPanel);
						viewComponent=null;
					}
					facade.removeCommand(CLZNoticeName.UPDATE_PERSON_ID_REQ);
					facade.removeMediator(SelectPersonMediator.NAME);
					break;
				}
				case CLZNoticeName.UPDATE_PERSON_ID_TIP:
				{
					personProp=notification.getBody() as PropVo;
					if (personProp.price == 0)
					{
						msg=GameData.tip["PERSON_TIP_FREE"];
					}
					else
					{
						msg=GameData.tip["PERSON_TIP_GOLD"];
					}
					msg=GameData.replaceStrTipHtml(msg);
					msg=GameData.replaceStrValue(msg, "GOLD", personProp.price);
					tipTitle=GameData.tip["PERSON_TIP_TITLE"];
					tipData={data: notification.getBody(), okNoticeName: CLZNoticeName.UPDATE_PERSON_ID_REQ, tip: msg, tipTitle: tipTitle, isCancel: true, okTip: "OK", cancelTip: "CANCEL", personProp: personProp};
					this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, tipData);
					break;
				}
				case CLZNoticeName.UPDATE_PERSON_ID_REQ:
				{
					this.sendNotification(CLZNoticeName.HIDE_WINDOWS_TIP);
					break;
				}
				case CLZNoticeName.UPDATE_PERSON_ID_RES:
				{
//					tipData=param1.getBody();
//					personProp=tipData.personProp;
//					trace("选择了："+personProp.pframe);
					this.sendNotification(CLZNoticeName.HIDE_SELECTED_PERSON);
					tipData=notification.getBody();
					if (tipData.errId == 0)
					{
//						this._global.userInfo.userVo.icon=tipData.ico;
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["PERSON_UPDATE_SUCC"]});
					}
					else
					{
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["PERSON_UPDATE_ERR"]});
					}

					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get mcPanel():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
